<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>OOP II in Ruby</title>
<link rel="stylesheet" href="/cfg/format.css" type="text/css">
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<meta name="keywords" content="Ruby, OOP, object-oriented programming, 
programming, language, learn Ruby">
<meta name="description" content="In this part of the Ruby tutorial, we continue covering 
Object-oriented programming in Ruby.">
<meta name="language" content="en">
<meta name="author" content="Jan Bodnar">
<meta name="distribution" content="global">

<script type="text/javascript" src="/lib/jquery.js"></script>
<script type="text/javascript" src="/lib/common.js"></script>

</head>

<body>

<div class="container2">

<div id="wide_ad" class="ltow">
<script type="text/javascript"><!--
google_ad_client = "pub-9706709751191532";
/* 160x600, August 2011 */
google_ad_slot = "2484182563";
google_ad_width = 160;
google_ad_height = 600;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<div class="content2">

<a href="/" title="Home">Home</a>&nbsp;
<a href="..">Contents</a>


<h1>Object-oriented programming II in Ruby</h1>

<p>
In this part of the Ruby tutorial, we will continue talking about object-oriented 
programming in Ruby.
</p>

<p>
We start with attribute accessors. We will cover class constants, 
class methods and operator overloading. We will define polymorphism and
will show how it is used in Ruby. We will also mention modules and exceptions.
</p>

<div class="big_hor">
<script type="text/javascript"><!--
google_ad_client = "ca-pub-9706709751191532";
/* big_horizontal */
google_ad_slot = "2904953388";
google_ad_width = 728;
google_ad_height = 90;
//-->
</script>
<script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js">
</script>
</div>


<h2>Attribute accessors</h2>

<p>
All Ruby variables are private. It is possible to access them only via methods.
These methods are often called setters and getters. Creating a setter and a getter
method is a very common task. Therefore Ruby has convenient methods
to create both types of methods. They are <code>attr_reader</code>, <code>attr_writer</code>
and <code>attr_accessor</code>.
</p>

<p>
The <code>attr_reader</code> creates getter methods. The <code>attr_writer</code>
method creates setter methods and instance variables for this setters. 
The <code>attr_accessor</code> method creates both getter, setter methods
and their instance variables. 
</p>

<pre class="code">
#!/usr/bin/ruby

class Car
    
    attr_reader :name, :price
    attr_writer :name, :price  
    
    def to_s
        "#{@name}: #{@price}"
    end

end


c1 = Car.new
c2 = Car.new

c1.name = "Porsche"
c1.price = 23500

c2.name = "Volkswagen"
c2.price = 9500

puts "The #{c1.name} costs #{c1.price}"

p c1
p c2
</pre>

<p>
We have a Car class. In the definition of the class, we use
the <code>attr_reader</code> and <code>attr_writer</code> to
create two getter and setter methods for the Car class. 
</p>

<pre class="explanation">
attr_reader :name, :price
</pre>

<p>
Here we create two instance methods named name and price.
Note that the <code>attr_reader</code> takes symbols of methods
as parameters. 
</p>

<pre class="explanation">
attr_writer :name, :price  
</pre>

<p>
The <code>attr_writer</code> creates two setter methods named
name and price and two instance variables @name and @price. 
</p>

<pre class="explanation">
c1.name = "Porsche"
c1.price = 23500
</pre>

<p>
In this context, two setter methods are called to fill
instance variables with some data. 
</p>

<pre class="explanation">
puts "The #{c1.name} costs #{c1.price}"
</pre>

<p>
Here two getter methods are called to get data from 
the instance variables of the c1 object. 
</p>

<pre>
$ ./arw.rb 
The Porsche costs 23500
Porsche: 23500
Volkswagen: 9500
</pre>

<p>
Output of the example.
</p>

<hr class="btm">

<p>
As we already stated above, the <code>attr_accessor</code> method
creates both getter, setter methods and their instance variables. 
</p>

<pre class="code">
#!/usr/bin/ruby

class Book
   attr_accessor :title, :pages    
end

b1 = Book.new
b1.title = "Hidden motives"
b1.pages = 255

p "The book #{b1.title} has #{b1.pages} pages"
</pre>

<p>
We have a Book class in which the <code>attr_accessor</code>
creates two pairs of methods and two instance variables. 
</p>

<pre class="explanation">
class Book
   attr_accessor :title, :pages    
end
</pre>

<p>
The <code>attr_accessor</code> method that sets up title and
pages methods and @title and @pages instance variables.
</p>

<pre class="explanation">
b1 = Book.new
b1.title = "Hidden motives"
b1.pages = 255
</pre>

<p>
An object of a Book class is created. Two setter methods
fill the instance variables of the object. 
</p>

<pre class="explanation">
p "The book #{b1.title} has #{b1.pages} pages"
</pre>

<p>
In this code line we use two getter methods to read
the values of the instance variables. 
</p>

<pre>
$ ./accessor.rb
"The book Hidden motives has 255 pages"
</pre>

<p>
Example output.
</p>


<h2>Class constants</h2>

<p>
Ruby enables to create class constants. These constants do not belong 
to a concrete object. They belong to the class. By convention, constants are 
written in uppercase letters. 
</p>

<pre class="code">
#!/usr/bin/ruby

class MMath

    PI = 3.141592
end


puts MMath::PI
</pre>

<p>
We have a MMath class with a PI constant. 
</p>

<pre class="explanation">
PI = 3.141592
</pre>

<p>
We create a PI constant. Remember that constants in Ruby
are not enforced. 
</p>

<pre class="explanation">
puts MMath::PI
</pre>

<p>
We access the PI constant using the :: operator. 
</p>

<pre>
$ ./classconstant.rb 
3.141592
</pre>

<p>
Running the example.
</p>


<h2>The to_s method</h2>

<p>
Each object has a <code>to_s</code> method. It returns a 
string representation of the object.  Note that when 
the <code>puts</code> method takes an object as a parameter, 
the <code>to_s</code> of the object is being called.
</p>

<pre class="code">
#!/usr/bin/ruby

class Being

    def to_s
        "This is Being class"
    end
end

b = Being.new
puts b.to_s
puts b
</pre>

<p>
We have a Being class in which we override the default implementation
of the <code>to_s</code> method. 
</p>

<pre class="explanation">
def to_s
    "This is Being class"
end
</pre>

<p>
Each class created inherits from the base <code>Object</code>. 
The <code>to_s</code> method belongs to this class. 
We overwrite the to_s method and create a new implementation.
We provide a human-readable description of our object.
</p>

<pre class="explanation">
b = Being.new
puts b.to_s
puts b
</pre>

<p>
We create a Being class and call the <code>to_s</code> method
twice. The first time explicitly, the second time implicitly.
</p>

<pre>
$ ./tostring.rb 
This is Being class
This is Being class
</pre>

<p>
This is what we get, when we run the example. 
</p>

<h2>Operator overloading</h2>

<p>
Operator overloading is a situation where different operators 
have different implementations depending on their arguments.
</p>

<p>
In Ruby there is only a slight distinction between an operator
and a method. 
</p>

<pre class="code">
#!/usr/bin/ruby

class Circle
   
    attr_accessor :radius
    
    def initialize r
        @radius = r
    end

    def +(other)
        Circle.new @radius + other.radius
    end
    
    def to_s
        "Circle with radius: #{@radius}"
    end
end


c1 = Circle.new 5
c2 = Circle.new 6
c3 = c1 + c2

p c3
</pre>

<p>
In the example, we have a Circle class. We overload the + operator
in the class. We use it to add two circle objects. 
</p>

<pre class="explanation">
def +(other)
    Circle.new @radius + other.radius
end
</pre>

<p>
We define a method with a + name. The method adds the radiuses
of two circle objects. 
</p>

<pre class="explanation">
c1 = Circle.new 5
c2 = Circle.new 6
c3 = c1 + c2
</pre>

<p>
We create two circle objects. In the third line, we add these
two objects to create a new one. 
</p>

<pre>
$ ./operatoroverloading.rb
Circle with radius: 11
</pre>

<p>
By adding two circle objects we have created a third one, which radius
is 11. 
</p>


<h2>Class methods</h2>

<p>
From a specific point of view, Ruby has two kinds of methods. Class methods and
instance methods. <b>Class methods</b> are called on a class. They cannot be
called on an instance of a class; on a created object. 
</p>

<p>
Class methods cannot access instance variables. 
</p>

<pre class="code">
#!/usr/bin/ruby

class Circle
    
    def initialize x
        @r = x
    end
   
    def self.info
       "This is a Circle class" 
    end
    
    def area
        @r * @r * 3.141592
    end

end


p Circle.info
c = Circle.new 3
p c.area
</pre>

<p>
The above code example presents a Circle class. Apart from a constructor method,
it has one class and one instance method. 
</p>

<pre class="explanation">
def self.info
    "This is a Circle class" 
end
</pre>

<p>
Methods that start with a <code>self</code> keyword are class methods. 
</p>

<pre class="explanation">
def area
    "Circle, radius: #{@r}"
end
</pre>

<p>
Instance methods do not start with the self keyword. 
</p>

<pre class="explanation">
p Circle.info
</pre>

<p>
We call a class method. Note that we call the method on a class name.
</p>

<pre class="explanation">
c = Circle.new 3
p c.area
</pre>

<p>
To call an instance method, we must first create an object. 
Instance methods are always called on an object. In our case, the
c variable holds the object and we call the area method on the
circle object. We utilize a dot operator. 
</p>

<pre>
$ ./classmethods.rb
"This is a Circle class"
28.274328
</pre>

<p>
Output of the code example describing class methods in Ruby.
</p>

<hr class="btm">

<p>
There are three ways to create a class method in Ruby.
</p>

<pre class="code">
#!/usr/bin/ruby

class Wood
     
    def self.info
       "This is a Wood class" 
    end
end

class Brick
     
    class &lt;&lt; self
        def info
           "This is a Brick class" 
        end
    end
end

class Rock
     
end

def Rock.info
   "This is a Rock class" 
end


p Wood.info
p Brick.info
p Rock.info
</pre>

<p>
The example has three classes. Each of them has one
class method. 
</p>

<pre class="explanation">
def self.info
    "This is a Wood class" 
end
</pre>

<p>
Class methods may start with a <code>self</code> keyword. 
</p>

<pre class="explanation">
class &lt;&lt; self
    def info
        "This is a Brick class" 
    end
end
</pre>

<p>
Another way is to put a method definition after the class &lt;&lt; self 
construct. 
</p>

<pre class="explanation">
def Rock.info
   "This is a Rock class" 
end
</pre>

<p>
This is the third way to define a class method in Ruby.
</p>

<pre>
$ ./classmethods2.rb
"This is a Wood class"
"This is a Brick class"
"This is a Rock class"
</pre>

<p>
We see the output of calling all three class methods on a
Wood, Brick and Rock classes.
</p>

<h2>Three ways to create an instance method</h2>

<p>
Ruby has three basic ways to create instance methods. 
Instance methods belong to an instance of an object. 
They are called on an object using a dot operator. 
</p>

<pre class="code">
#!/usr/bin/ruby

class Wood
     
    def info
       "This is a wood object"        
    end
end

wood = Wood.new
p wood.info

class Brick
     
    attr_accessor :info
end

brick = Brick.new
brick.info = "This is a brick object"
p brick.info

class Rock
     
end

rock = Rock.new

def rock.info
    "This is a rock object"
end

p rock.info
</pre>

<p>
In the example we create three instance objects from a Wood,
a Brick and a Rock class. Each object has one instance method
defined. 
</p>

<pre class="explanation">
class Wood
     
    def info
       "This is a wood object"        
    end
end

wood = Wood.new
p wood.info
</pre>

<p>
This is probably the most common way to define and call
an instance method. The info method is defined inside the 
Wood class. Later, the object is created and we call the 
info method on the object instance. 
</p>

<pre class="explanation">
class Brick
     
    attr_accessor :info
end

brick = Brick.new
brick.info = "This is a brick object"
p brick.info
</pre>

<p>
Another way is to create a method using the attribute
accessors. This is a convenient way which saves some 
typing for the programmer. The <code>attr_accessor</code> creates
two methods, the getter and the setter method and it also creates
an instance variable which stores the data. The brick object is created
and the data is stored in the @info variable using the info setter
method. Finally, the message is read by the info getter method.
</p>

<pre class="explanation">
class Rock
     
end

rock = Rock.new

def rock.info
    "This is a rock object"
end

p rock.info
</pre>

<p>
In the third way we create an empty Rock class. The
object is instantiated. Later, a method is dynamically
created and placed into the object. 
</p>


<pre>
$ ./threeways.rb
"This is a wood object"
"This is a brick object"
"This is a rock object"
</pre>

<p>
Example output.
</p>


<h2>Polymorphism</h2>

<p>
The <b>polymorphism</b> is the process of using an operator or function 
in different ways for different data input. In practical terms, 
polymorphism means that if class B inherits from class A, 
it doesn't have to inherit everything about class A; it can do some 
of the things that class A does differently. (Wikipedia)
</p>

<p>
In general, polymorphism is the ability to appear in different forms. 
Technically, it is the ability to redefine methods for derived classes. 
Polymorphism is concerned with the application of specific implementations 
to an interface or a more generic base class.
</p>

<p>
Note that there is some difference in the definition of the polymorphism 
in statically typed languages like C++, Java or C# and dynamically 
typed languages like Python or Ruby. In statically typed languages it is
important when the compilers determine the method definition. At compile time
or at run time. In dynamically typed languages we concentrate on the fact, 
that methods with the same name do different things. 
</p>

<pre class="code">
#!/usr/bin/ruby

class Animal
    
    def make_noise 
        "Some noise"
    end

    def sleep 
        puts "#{self.class.name} is sleeping." 
    end
  
end

class Dog &lt; Animal
    
    def make_noise 
        'Woof!'         
    end 
    
end

class Cat &lt; Animal 
    
    def make_noise 
        'Meow!' 
    end 
end

[Animal.new, Dog.new, Cat.new].each do |animal|
  puts animal.make_noise
  animal.sleep
end
</pre>

<p>
We have a simple inheritance hierarchy. There is an Animal base class
and two descendants, a Cat and a Dog. Each of these three classes has 
its own implementation of the make_noise method. The implementation of
the method of the descendants replaces the definition of a method in 
the Animal class. 
</p>

<pre class="explanation">
class Dog &lt; Animal
    
    def make_noise 
        'Woof!'         
    end 
    
end
</pre>

<p>
The implementation of the make_noise method in the Dog class
replaces the implementation of the make_noise of the Animal class.
</p>

<pre class="explanation">
[Animal.new, Dog.new, Cat.new].each do |animal|
  puts animal.make_noise
  animal.sleep
end
</pre>

<p>
We create an instance of ech of the classes. We call 
make_noise and sleep methods on the objects. 
</p>

<pre>
$ ./polymorhism.rb
Some noise
Animal is sleeping.
Woof!
Dog is sleeping.
Meow!
Cat is sleeping.
</pre>

<p>
Output of the polymorhism.rb script.
</p>

<h2>Modules</h2>

<p>
A Ruby <code>Module</code> is a collection of methods, classes and constants.
Modules are similar to classes with a few differences. Modules cannot
have instances. They have no subclasses.
</p>

<p>
Modules serve two basic purposes. They are used to organize code. Classes,
methods and constants which have something in common can be put into separate
modules. This also prevents name clashes, because all objects are unique by
their module. In this context, Ruby modules are similar to C# namespaces and
Java packages. 
</p>

<p>
The second purpose of a module is to create <b>mixins</b>. A mixin is a Ruby
facility to create <b>multiple inheritance</b>. If a class inherits functionality
from more than one class, we speak of multiple inheritance. 
</p>

<pre class="code">
#!/usr/bin/ruby

puts Math::PI
puts Math.sin 2
</pre>

<p>
Ruby has a built-in <code>Math</code> module. It has multiple
methods and a constant. We access the PI constant by using the :: operator.
Methods are accessed by a dot operator as in classes. 
</p>

<pre class="code">
#!/usr/bin/ruby

include Math

puts PI
puts sin 2
</pre>

<p>
If we include a module in our script, we can refer to the
Math objects directly, omitting the Math name. Modules are added
to a script using the <code>include</code> keyword. 
</p>

<pre>
$ ./modules.rb
3.141592653589793
0.9092974268256817
</pre>

<p>
Output of the program.
</p>

<hr class="btm">

<p>
In the following example, we show how modules can be used to organize code.
</p>

<pre class="code">
#!/usr/bin/ruby

module Forest
 
    class Rock ; end
    class Tree ; end
    class Animal ; end    
    
end

module Town
    
   class Pool ; end
   class Cinema ; end
   class Square ; end
   class Animal ; end
        
end


p Forest::Tree.new
p Forest::Rock.new
p Town::Cinema.new

p Forest::Animal.new
p Town::Animal.new
</pre>

<p>
Ruby code can be grouped semantically. Rocks, trees belong to a forest.
And pools, cinemas, squares belong to a town. By using modules our code has
some order. Plus there is another thing. Animals can be in a forest and in 
a town too. In a single script, we cannot define two animal classes. They would
clash. Putting them in different modules we solve the issue. 
</p>

<pre class="explanation">
p Forest::Tree.new
p Forest::Rock.new
p Town::Cinema.new
</pre>

<p>
We are creating objects that belong to a forest and to a town. To access
an object in a module, we use the :: operator. 
</p>

<pre class="explanation">
p Forest::Animal.new
p Town::Animal.new
</pre>

<p>
Two different animal objects are created. The Ruby interpreter can tell between them.
It identifies them by their module name. 
</p>

<pre>
$ ./modules3.rb
#&lt;Forest::Tree:0x97f35ec&gt;
#&lt;Forest::Rock:0x97f35b0&gt;
#&lt;Town::Cinema:0x97f3588&gt;
#&lt;Forest::Animal:0x97f3560&gt;
#&lt;Town::Animal:0x97f3538&gt;
</pre>

<p>

</p>

<hr class="btm">

<p>
The final code example of this section will demonstrate
multiple inheritance using Ruby modules. In this context the modules
are called mixins. 
</p>

<pre class="code">
#!/usr/bin/ruby

module Device
    def switch_on ; puts "on" end    
    def switch_off ; puts "off" end
end

module Volume
    def volume_up ; puts "volume up" end    
    def vodule_down ; puts "volume down" end
end

module Pluggable
    def plug_in ; puts "plug in" end    
    def plug_out ; puts "plug out" end
end

class CellPhone
    include Device, Volume, Pluggable
   
    def ring
        puts "ringing"
    end    
end

cph = CellPhone.new
cph.switch_on
cph.volume_up
cph.ring
</pre>

<p>
We have three modules and one class. The modules represent some functionality.
A device can be swiched on and off. Many objects can share this functionality.
Televisions, mobile phones, computers or refrigerators. Rather than creating
this ability to be swiched on/off for each object class, we separate it in one
module, which can be included in each object if necessary. This way the code is
better organized and more compact. 
</p>

<pre class="explanation">
module Volume
    def volume_up ; puts "volume up" end    
    def vodule_down ; puts "volume down" end
end
</pre>

<p>
A Volume module organizes methods, than are responsible for controlling the
volume level. If a device needs these methods, it simply includes the module
to its class. 
</p>

<pre class="explanation">
class CellPhone
    include Device, Volume, Pluggable
   
    def ring
        puts "ringing"
    end    
end

</pre>

<p>
A cell phone adds all three modules with the <code>include</code> method.
The methods of the modules are mixed in the CellPhone class. And
are available for the instances of the class. The CellPhone class
has also its own ring method that is specific to it.
</p>

<pre class="explanation">
cph = CellPhone.new
cph.switch_on
cph.volume_up
cph.ring
</pre>

<p>
A CellPhone object is created and we call three methods upon the object. 
</p>

<pre>
$ ./mixins.rb
on
volume up
ringing
</pre>

<p>
Running the example. 
</p>


<h2>Exceptions</h2>

<p>
Exceptions are designed to handle the occurrence of exceptions, 
special conditions that change the normal flow of program execution. 
Exceptions are raised, thrown or initiated.
</p>

<p>
During the execution of our application, many things might go wrong. 
A disk might get full and we cannot save our file. An Internet connection 
might go down and our application tries to connect to a site. All these 
might result in a crash of our application. To prevent happening this, 
we must cope with all possible errors that might occur. For this, we 
can use the exception handling.
</p>

<p>
Exceptions are objects. They are descendants of a built-in <code>Exception</code>
class. Exception objects carry information about the exception. Its type 
(the exception’s class name), an optional descriptive string, and optional 
traceback information. Programs may subclass <code>Exception</code>, or more often
<code>StandardError</code> to provide custom classes and add additional information.
</p>


<pre class="code">
#!/usr/bin/ruby

x = 35
y = 0

begin
    z = x / y
    puts z
rescue => e
    puts e
    p e
end
</pre>

<p>
In the above program, we intentionally divide a number by zero. This leads to an error.
</p>

<pre class="explanation">
begin
    z = x / y
    puts z
</pre>

<p>
Statements that are error prone are placed after the <code>begin</code> keyword.
</p>

<pre class="explanation">
rescue => e
    puts e
    p e
end
</pre>

<p>
In the code following the <code>rescue</code> keyword, we deal with an exception. 
In this case, we print the error message to the console. The e is an exception object
that is created when the error occurs. 
</p>

<pre>
$ ./zerodivision.rb
divided by 0
#&lt;ZeroDivisionError: divided by 0&gt;
</pre>

<p>
In the output of the example, we see the message of the exception. The
last line shows the exception object called ZeroDivisionError.
</p>

<hr class="btm">

<p>
A programmer may raise exceptions himself using the <code>raise</code> keyword.
</p>

<pre class="code">
#!/usr/bin/ruby

age = 17

begin
    if age &lt; 18
        raise "Person is minor"
    end
    
    puts "Entry allowed"
rescue => e
    puts e
    p e
    exit 1
end
</pre>

<p>
The entrance to a club is not allowed for people younger than 18 years.
We simulate this situation in our Ruby script.
</p>

<pre class="explanation">
begin
    if age &lt; 18
        raise "Person is minor"
    end
    
    puts "Entry allowed"
</pre>

<p>
If the person is minor, an exception is raised. If the <code>raise</code> 
keyword does not have a specific exception as a parameter,  a  RuntimeError 
exception is raised setting its message to the given string. The code does
not reach the <code>puts "Entry allowed"</code> line. The execution of the code
is interrupted and it continues at the rescue block.
</p>

<pre class="explanation">
rescue => e
    puts e
    p e
    exit 1
end
</pre>

<p>
In the rescue block, we print the error message and the string representation
of the <code>RuntimeError</code> object. We also call the <code>exit</code> method
to inform the environment that the execution of the script ended in error. 
</p>


<pre>
$ ./raise_exception.rb
Person is minor
#&lt;RuntimeError: Person is minor&gt;
$ echo $?
1
</pre>

<p>
The person was minor and he or she was not allowed to enter the club.
The bash $? variable is set to the exit error of the script. 
</p>

<hr class="btm">

<p>
With the Ruby <code>ensure</code> clause we create a block of code
that is always executed. Whether there is an exception or not. 
</p>

<pre class="code">
#!/usr/bin/ruby

begin
    f = File.open("stones", "r")
    
    while line = f.gets do
        puts line
    end    
    
rescue => e
    puts e
    p e
ensure
    f.close if f
end
</pre>

<p>
In the code example, we try to open and read the
stones file. I/O operations are error prone. We could
easily have an exception. 
</p>

<pre class="explanation">
ensure
    f.close if f
end
</pre>

<p>
In the ensure block we close the file handler. 
We check if the handler exists because it might not have been created.
Allocated resources are often placed in the ensure block.
</p>

<hr class="btm">

<p>
We can create our own custom exceptions if we want.
Custom exceptions in Ruby should inherit from the 
<code>StandardError</code> class. 
</p>

<pre class="code">
#!/usr/bin/ruby

class BigValueError &lt; StandardError ; end

LIMIT = 333
x = 3_432_453
    
begin
    
    if x > LIMIT
        raise BigValueError, "Exceeded the maximum value"
    end
        
    puts "Script continues"
    
rescue => e
    puts e
    p e
    exit 1
end
</pre>

<p>
Let's say, we have a situation in which we cannot deal with big numbers.
</p>

<pre class="explanation">
class BigValueError &lt; StandardError ; end
</pre>

<p>
We have a BigValueError class. This class derives from the built-in 
<code>StandardError</code> class.
</p>

<pre class="explanation">
LIMIT = 333
</pre>

<p>
Numbers which exceed this constant are considered to be "big" by our program.
</p>

<pre class="explanation">
if x > LIMIT
    raise BigValueError, "Exceeded the maximum value"
end
</pre>

<p>
If the value is bigger than the limit, we throw our custom exception. 
We give the exception a message "Exceeded the maximum value".
</p>

<pre>
$ ./custom_exception.rb
Exceeded the maximum value
#&lt;BigValueError: Exceeded the maximum value&gt;
</pre>

<p>
Running the program. 
</p>

<p>
In this chapter we finished talking about object-oriented programming
in Ruby language.
</p>


<div class="botNav, center">
<span class="botNavItem"><a href="/">Home</a></span> ‡ <span class="botNavItem"><a href="..">Contents</a></span> ‡
<span class="botNavItem"><a href="#">Top of Page</a></span>
</div>


<div class="footer">
<div class="signature">
<a href="/">ZetCode</a> last modified December 6, 2011  <span class="copyright">&copy; 2007 - 2013 Jan Bodnar</span>
</div>
</div>

</div> <!-- content -->

</div> <!-- container -->

</body> 
</html>

